package rp.lee.callsdecline.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rp.lee.callsdecline.R;
import rp.lee.callsdecline.activity.ContactsAddActivity;
import rp.lee.callsdecline.activity.DeclineListActivity;
import rp.lee.callsdecline.core.db.model.DeclineList;
import rp.lee.callsdecline.utils.DateUtils;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class DeclineListAdapter extends BaseAdapter{

	List<DeclineList> list;
	Context mContext;
	Map<Integer, View> cacheMap = new HashMap<Integer, View>();
	
	DeclineListActivity dlAct;
	ContactsAddActivity caAct;
	public DeclineListAdapter(List<DeclineList> list, Context mContext) {
		this.list = list;
		if(this.list == null) {
			this.list = new ArrayList<DeclineList>();
		}
		this.mContext = mContext;
		
		if(this.mContext instanceof DeclineListActivity) {
			dlAct = (DeclineListActivity)this.mContext;
		} else if(this.mContext instanceof ContactsAddActivity) {
			caAct = (ContactsAddActivity)this.mContext;
		}
	}
	
	@Override
	public int getCount() {
		return list.size();
	}
	
	@Override
	public void notifyDataSetChanged() {
		cacheMap.clear();
		super.notifyDataSetChanged();
	}
	
	@Override
	public void notifyDataSetInvalidated() {
		cacheMap.clear();
		super.notifyDataSetInvalidated();
	}
	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		if(cacheMap.containsKey(position)) {
			return cacheMap.get(position);
		}
		
		final DeclineList dl = list.get(position);
		LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
		View view = mLayoutInflater.inflate(R.layout.declinelist_item, null);

		TextView declinelist_name = (TextView) view.findViewById(R.id.declinelist_name);
		TextView declinelist_phonenumber = (TextView) view.findViewById(R.id.declinelist_phonenumber);
		TextView declinelist_date = (TextView) view.findViewById(R.id.declinelist_date);
		
		declinelist_name.setText(dl.getName());
		declinelist_phonenumber.setText(dl.getPhoneNumber());
		declinelist_date.setText(dl.getDate());
		
		ImageView declinelist_iv_edit = (ImageView) view.findViewById(R.id.declinelist_iv_edit);
		ImageView declinelist_iv_delete = (ImageView) view.findViewById(R.id.declinelist_iv_delete);
		ImageView declinelist_iv_phone = (ImageView) view.findViewById(R.id.declinelist_iv_phone);
		
		if(dlAct != null) {
			declinelist_iv_delete.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					dlAct.delete(dl);
				}
			});
			
			declinelist_iv_edit.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					dlAct.edit(dl);
				}
			});
		} else if(caAct != null) {
			declinelist_iv_edit.setImageResource(android.R.drawable.ic_menu_info_details);
			declinelist_iv_delete.setImageResource(android.R.drawable.ic_menu_add);
			
			declinelist_date.setText(DateUtils.nowDate());
		}
		
		declinelist_iv_phone.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
//				Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + dl.getPhoneNumber().sp));
			}
		});
		view.setTag(dl);
		
		cacheMap.put(position, view);
		return view;
	}

}